package com.cn.book;

import com.cn.book.services.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@EnableFeignClients
public class ConsulConsumerRibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsulConsumerRibbonApplication.class, args);
    }

    @Autowired
    private HelloService helloService;

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RequestMapping(path = "/say1")
    public String say1(@RequestParam("name") String name) {
        return helloService.say(name);
    }

    @RequestMapping(path = "/say2")
    public String say2(@RequestParam("name") String name) {
        return restTemplate.getForObject("http://consul-provider/say?name=" + name, String.class);
    }
}
